﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace Vincent
{
    /// <summary>
    /// web操作中的常用方法
    /// </summary>
    public class _Request
    {
        /// <summary>
        /// 获取地址栏参数,且转换为整数型j
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static int GetInt(string query)
        {
            return GetInt(query, 0);
        }

        /// <summary>
        /// 获取地址栏参数,且转换为整数型
        /// </summary>
        /// <param name="query"></param>
        /// <param name="ndefault"></param>
        /// <returns></returns>
        public static int GetInt(string query, int ndefault)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request[query]))
            {
                return _Convert.ToInt(ReplaceValue(HttpContext.Current.Request[query]), ndefault);
            }
            return ndefault;
        }

        /// <summary>
        /// 获取地址栏参数,且转换为长整数型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static long GetInt64(string query)
        {
            return GetInt64(query, 0L);
        }

        /// <summary>
        /// 获取地址栏参数,且转换为长整数型
        /// </summary>
        /// <param name="query"></param>
        /// <param name="ldefault"></param>
        /// <returns></returns>
        public static long GetInt64(string query, long ldefault)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request[query]))
            {
                return _Convert.ToInt64(ReplaceValue(HttpContext.Current.Request[query]), ldefault);
            }
            return ldefault;
        }

        /// <summary>
        /// 获取地址栏参数,且转换为float型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static float GetFloat(string query)
        {
            return GetFloat(query, 0);
        }

        /// <summary>
        /// 获取地址栏参数,且转换为float型
        /// </summary>
        /// <param name="query"></param>
        /// <param name="ndefault"></param>
        /// <returns></returns>
        public static float GetFloat(string query, float ndefault)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request[query]))
            {
                return _Convert.ToFloat(ReplaceValue(HttpContext.Current.Request[query]), ndefault);
            }
            return ndefault;
        }

        /// <summary>
        /// 获取post提交值,转换为float型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static float GetFloatForm(string query)
        {
            return GetFloatForm(query, 0);
        }

        /// <summary>
        /// 获取post提交值,转换为float型
        /// </summary>
        /// <param name="query"></param>
        /// <param name="ndefault"></param>
        /// <returns></returns>
        public static float GetFloatForm(string query, float ndefault)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request.Form[query]))
            {
                return _Convert.ToFloat(ReplaceValue(HttpContext.Current.Request.Form[query]), ndefault);
            }
            return ndefault;
        }

        /// <summary>
        /// 获取地址栏参数,且转换为double型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static double GetDouble(string query)
        {
            return GetDouble(query, 0);
        }

        /// <summary>
        /// 获取地址栏参数,且转换为double型
        /// </summary>
        /// <param name="query"></param>
        /// <param name="ndefault"></param>
        /// <returns></returns>
        public static double GetDouble(string query, double ndefault)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request[query]))
            {
                return _Convert.ToDouble(ReplaceValue(HttpContext.Current.Request[query]), ndefault);
            }
            return ndefault;
        }

        /// <summary>
        /// 获取post提交值,转换为double型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static double GetDoubleForm(string query)
        {
            return GetDoubleForm(query, 0);
        }

        /// <summary>
        /// 获取post提交值,转换为double型
        /// </summary>
        /// <param name="query"></param>
        /// <param name="ndefault"></param>
        /// <returns></returns>
        public static double GetDoubleForm(string query, double ndefault)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request.Form[query]))
            {
                return _Convert.ToDouble(ReplaceValue(HttpContext.Current.Request.Form[query]), ndefault);
            }
            return ndefault;
        }

        /// <summary>
        /// 获取地址栏参数
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static string GetString(string query)
        {
            return GetString(query, "");
        }

        /// <summary>
        /// 获取地址栏参数
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strdefault"></param>
        /// <returns></returns>
        public static string GetString(string query, string strdefault)
        {
            string str = strdefault;
            if (HttpContext.Current.Request[query] != null)
            {
                str = HttpUtility.UrlDecode(HttpContext.Current.Request[query].Trim());
            }
            return str;
        }

        /// <summary>
        /// 获取post请求值，转换为长整型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static long GetInt64Form(string query)
        {
            return GetInt64Form(query, 0L);
        }

        /// <summary>
        /// 获取post请求值，转换为长整型
        /// </summary>
        /// <param name="query"></param>
        /// <param name="ldefault"></param>
        /// <returns></returns>
        public static long GetInt64Form(string query, long ldefault)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request.Form[query]))
            {
                return _Convert.ToInt64(ReplaceValue(HttpContext.Current.Request.Form[query]), ldefault);
            }
            return ldefault;
        }

        /// <summary>
        /// 获取post提交值,转换为int型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static int GetIntForm(string query)
        {
            return GetIntForm(query, 0);
        }

        /// <summary>
        /// 获取post提交值,转换为int型
        /// </summary>
        /// <param name="query"></param>
        /// <param name="ndefault"></param>
        /// <returns></returns>
        public static int GetIntForm(string query, int ndefault)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request.Form[query]))
            {
                return _Convert.ToInt(ReplaceValue(HttpContext.Current.Request.Form[query]), ndefault);
            }
            return ndefault;
        }

        /// <summary>
        /// 获取post提交值
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static string GetStringForm(string query)
        {
            return GetStringForm(query, "");
        }

        /// <summary>
        /// 获取post提交值
        /// </summary>
        /// <param name="query"></param>
        /// <param name="strdefault"></param>
        /// <returns></returns>
        public static string GetStringForm(string query, string strdefault)
        {
            string str = strdefault;
            if (HttpContext.Current.Request.Form[query] != null)
            {
                str = HttpContext.Current.Request.Form[query].Trim();
            }
            return str;
        }

        /// <summary>
        /// 获取地址栏参数,且转换为 bool 型
        /// </summary>
        /// <param name="query"></param>
        /// <returns></returns>
        public static bool GetBool(string query)
        {
            return GetBool(query, false);
        }

        /// <summary>
        /// 获取地址栏参数,且转换为 bool 型
        /// </summary>
        /// <param name="query"></param>
        /// <param name="ndefault"></param>
        /// <returns></returns>
        public static bool GetBool(string query, bool ndefault)
        {
            if (!string.IsNullOrEmpty(HttpContext.Current.Request[query]))
            {
                return _Convert.ToBool(ReplaceValue(HttpContext.Current.Request[query]), ndefault);
            }
            return ndefault;
        }

        private static string ReplaceValue(string value)
        {
            int index = value.IndexOf(",", StringComparison.Ordinal);
            if (index > 0)
            {
                return value.Substring(0, index);
            }
            if (index == 0)
            {
                return "";
            }
            return value;
        }

    }
}
